from django.http import JsonResponse
from django.views.generic import View
from django.shortcuts import redirect, reverse
from app.libs.base_render import render_to_response
from app.model.auth import ClientUser, hash_password, Collect
from app.model.video import Video


# 注册信息的地方
class Regist(View):
    TEMPLATE = 'client/auth/userregist.html'
    TEMPLATE2 = 'client/auth/userlogin.html'

    def get(self, request):

        data = {'error': ''}
        print(hash_password('a'))
        return render_to_response(request, self.TEMPLATE, data=data)

    def post(self, request):
        print('1')
        data = {}
        username = request.POST.get('username', '')
        password = request.POST.get('password', '')
        repassword = request.POST.get('repassword', '')
        gender = request.POST.get('gender', '')
        age = request.POST.get('age', '')
        job = request.POST.get('job', '')
        email = request.POST.get('email', '')

        if not username or not password or not age or not job or not email:
            error = '缺少信息'
            return render_to_response(request, self.TEMPLATE, data={'error': error})

        exists = ClientUser.objects.filter(username=username).exists()
        print('2')
        if exists:
            error = '该用户已存在'
            return render_to_response(request, self.TEMPLATE, data={'error': error})

        if len(password) < 3:
            error = '密码太短'
            return render_to_response(request, self.TEMPLATE, data={'error': error})

        if password != repassword:
            error = '两次密码不一致'
            return render_to_response(request, self.TEMPLATE, data={'error': error})

        # 添加用户
        ClientUser.add(username=username, password=password, gender=gender, age=age, job=job, email=email)
        print('3')

        # 跳转页面
        return render_to_response(request, self.TEMPLATE2)


# 登录逻辑处理
class Login(View):
    TEMPLATE = 'client/auth/userlogin.html'

    def get(self, request):

        if request.user.is_authenticated:
            return redirect(reverse('client_video'))

        data = {'error': ''}
        return render_to_response(request, self.TEMPLATE, data)

    def post(self, request):

        username = request.POST.get('username')
        password = request.POST.get('password')
        data = {}

        if not password or not username:
            data['error'] = '不可为空'
            return render_to_response(request, self.TEMPLATE, data=data)

        # exists = User.objects.filter(username=username).exists()
        # data['error'] = '没有该用户'
        # if not exists:
        #     return render_to_response(request, self.TEMPLATE, data=data)
        exist = ClientUser.objects.filter(username=username).exists()

        if not exist:
            data['error'] = '该用户不存在'
            return render_to_response(request, self.TEMPLATE, data=data)

        user = ClientUser.objects.get(username=username)

        if not user.password == hash_password(password):
            data['error'] = '密码错误'
            return render_to_response(request, self.TEMPLATE, data=data)

        if not user.status:
            data['error'] = '该账号已被封'
            return render_to_response(request, self.TEMPLATE, data=data)
        else:
            request.session['username'] = username
            ## 跳转到视频页
            return redirect(reverse('client_video'))


# 登出逻辑
class Logout(View):

    def get(self, request):
        request.session.clear()
        return redirect(reverse('client_login'))


# 修改用户信息的地方
class UpdateInfo(View):
    TEMPLATE = 'client/auth/show.html'

    def get(self, request):
        username = request.session['username']
        user = ClientUser.objects.get(username=username)
        data = {'user': user}
        return render_to_response(request, self.TEMPLATE, data=data)


class Update(View):
    TEMPLATE = 'client/auth/update.html'
    TEMPLATE3 = 'client/auth/success.html'

    def get(self, request):
        username = request.session['username']
        user = ClientUser.objects.get(username=username)
        error = request.GET.get('error', '')
        data = {'user': user, 'error': error}
        return render_to_response(request, self.TEMPLATE, data=data)

    def post(self, request):
        new_username = request.POST.get('username')
        old_username = request.session['username']
        password = request.POST.get('password')
        gender = request.POST.get('gender')
        age = request.POST.get('age')
        email = request.POST.get('email')
        job = request.POST.get('job')

        if not all([new_username, password, gender, age, email, job]):
            error = '不能有空信息'
            username = request.session['username']
            user = ClientUser.objects.get(username=username)
            data = {'user': user, 'error': error}
            return render_to_response(request, self.TEMPLATE, data=data)

        if old_username == new_username:
            ClientUser.objects.filter(username=old_username).update(password=hash_password(password))
            ClientUser.objects.filter(username=old_username).update(gender=gender)
            ClientUser.objects.filter(username=old_username).update(age=age)
            ClientUser.objects.filter(username=old_username).update(email=email)
            ClientUser.objects.filter(username=old_username).update(job=job)
            return render_to_response(request, self.TEMPLATE3)

        if new_username != old_username and ClientUser.objects.filter(username=new_username).count() == 1:
            error = '该用户名已存在，请重新输入'
            username = request.session['username']
            user = ClientUser.objects.get(username=username)
            data = {'user': user, 'error': error}
            return render_to_response(request, self.TEMPLATE, data=data)
        else:
            ClientUser.objects.filter(username=old_username).update(password=hash_password(password))
            ClientUser.objects.filter(username=old_username).update(gender=gender)
            ClientUser.objects.filter(username=old_username).update(age=age)
            ClientUser.objects.filter(username=old_username).update(email=email)
            ClientUser.objects.filter(username=old_username).update(job=job)
            ClientUser.objects.filter(username=old_username).update(username=new_username)

            request.session['username'] = new_username
            return render_to_response(request, self.TEMPLATE3)


class ShowCollect(View):
    TEMPLATE = 'client/auth/collect.html'

    def get(self, request):
        print('654655616523')
        username = request.session['username']
        sub = []
        user = ClientUser.objects.filter(username=username).first()
        movieidobject = Collect.objects.filter(username=user)
        if movieidobject:
            for a in movieidobject:
                t = Video.objects.filter(movieId=a.movieId).first()
                sub.append(t)
            data = {'sub': sub}
            return render_to_response(request, self.TEMPLATE, data=data)
        else:
            data = {'msg': '您还没有收藏电影'}
            return render_to_response(request, self.TEMPLATE, data=data)


class CancelUpdate(View):

    def get(self, request):
        movieId = request.GET.get('movieid', '')
        username = request.session['username']
        user = ClientUser.objects.filter(username=username).first()
        ready_to_delete = Collect.objects.filter(username=user, movieId=movieId).first()
        ready_to_delete.delete()
        data = {'msg': '取消收藏成功'}
        return JsonResponse(data)
